package com.hotel.backstage.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hotel.jabc.DB;
import com.hotel.jabc.Order_DB;

public class OrderServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void init(ServletConfig conf)throws ServletException
	{
		super.init(conf);
	}
	@Override
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		doPost(req, res);
	}
	@Override
	public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		req.setCharacterEncoding("utf-8");
		res.setCharacterEncoding("utf-8");
		res.setContentType("text/html");
		String action = req.getParameter("action");
		//得到请求的响应action
		HttpSession session = req.getSession(true);
		//得到session对象
		PrintWriter pw = res.getWriter(); 
		//得到输出流对象
		String msg=""; //声明提示消息
		String uname = (String) session.getAttribute("uname");
		//得到登录用户名
		String ostatus1 = "预定中"; //资源的几种状态
		String ostatus2 = "预订成功";
		String ostatus3 = "预订失败";
		Vector<String[]> OrderList =//得到订单列表
					(Vector<String[]>)session.getAttribute("OrderList");
		if(OrderList == null)//如果为空则创建一个订单列表对象
		{
			OrderList = new Vector<String[]>();
		}
		if(action.equals("ADD"))
		{
			String orderNum = req.getParameter("orderNum");
			//得到资源名
			if(orderNum!=null)//预订开始时间
			{
				String fyear = req.getParameter("fyear");
				String fmonth = req.getParameter("fmonth");
				String fday = req.getParameter("fday");
				String fhour = req.getParameter("fhour");
				//得到预订结束时间
				String eyear = req.getParameter("eyear");
				String emonth = req.getParameter("emonth");
				String eday = req.getParameter("eday");
				String ehour = req.getParameter("ehour");
				//获取预订的资源分组
				String group = req.getParameter("group");
				String ftime = fyear+"-"+fmonth+"-"+fday+"-"+fhour+":"+"00";
				String etime = eyear+"-"+emonth+"-"+eday+"-"+ehour+":"+"00";
				String[] s = new String[4];
				s[0] = orderNum; s[1] = group;
				s[2] = ftime; 	 s[3] = etime;
				OrderList.add(s);//将订单加入到订单列表中
				session.setAttribute("OrderList", OrderList); //将订单列表放到session中
				
			}
			res.sendRedirect("../jsp/groupList.jsp"); //发回到groupList页面
		}
		else if(action.equals("DELETE"))
		{
			String id = req.getParameter("index");//获取要删除订单的下标
			int index = Integer.parseInt(id);//转换为int值
			OrderList.remove(index);//删除此选中的订单
			res.sendRedirect("../jsp/myOrder.jsp");//发送回订单页面
		}
		else if(action.equals("REMOVE"))
		{
			OrderList.removeAllElements();//移除所有订单
			session.setAttribute("OrderList",OrderList);//将订单列表放进session
			res.sendRedirect("../jsp/myOrder.jsp");//发回订单页面
		}
		else if(action.equals("SUBMIT"))
		{
			if(uname!=null)//判断用户是否登录
			{
				if(Order_DB.addOrder(uname, OrderList)!=-1)
				{
					OrderList.removeAllElements();//提交完成，清空订单列表
					msg = "订单提交成功，本酒店欢迎您的光临<br><br>"+
							"<a href=jsp/main.jsp>返回主页</a>";//给出成功消息
				}
				else{
					msg= "对不起，系统错误，提交失败<br><br>"+
							"<a href=jsp/main.jsp>返回主页</a>";//给出失败消息
				}
			}
			else{//用户没有登录的情况
				msg= "您还没有登录，请先登录<br><br>"+
						"<a href=../login.jsp>现在登录</a>";
			}
			req.setAttribute("msg",msg);//将消息发送到消息显示界面
			req.getRequestDispatcher("../jsp/info.jsp").forward(req, res);
		}
		else if(action.equals("order"))
		{
			if(uname==null)//判断用户是否登录
			{
				msg = "您还没有登录，请先登录<br><br>"+
						"<a href=jsp/login.jsp>现在登录</a>";
				req.setAttribute("msg", msg);
				req.getRequestDispatcher("../jsp/info.jsp").forward(req, res);
			}
			else{
				//查询该用户的所有订单信息
				String sql = "select * from olist where oname='"+uname+"'";
				Vector<String []> list = Order_DB.getOrderList(sql);//执行查询
				req.setAttribute("list",list);
				req.getRequestDispatcher("../jsp/list.jsp").forward(req, res);
			}
		}
		else if(action.equals("ListDetail"))
		{
			String oid = req.getParameter("oid");//获取订单编号
			Vector<String []> ListDetail = Order_DB.getOrderDetail(oid);//执行查询
			req.setAttribute("ListDetail", ListDetail);
			req.setAttribute("oid", oid);
			req.getRequestDispatcher("../jsp/detail.jsp").forward(req, res);
			
	
		}
		else if(action.equals("allOrders"))//按条件查询订单
		{
			if(session.getAttribute("adname")!=null)//管理员是否登录
			{
				String sql = "";//声明SQL引用
				int condition = Integer.parseInt(req.getParameter("condition"));
				switch (condition) 
				{
					case 1://表示所有订单
						sql = "select * from olist";
						break;
					case 2://表示已经处理的订单
						sql = "select * from olist where ostatus='"+ostatus2+"' or ostatus='"+ostatus3+"'";
						break;
					case 3://表示未处理的订单
						sql = "select * from olist where ostatus='"+ostatus1+"'";
						break;
				}
				Vector<String []> list = Order_DB.getOrderList(sql);
				req.setAttribute("list",list);//将订单列表返回
				req.getRequestDispatcher("../jsp/adminOrders.jsp").forward(req, res);
			}
			else{
				msg = "请先登录";//没有登录的提示消息
				req.setAttribute("msg",msg);
				req.getRequestDispatcher("../jsp/adinfo.jsp").forward(req,res);
			}
			
		}
		else if(action.equals("query"))//按编号查询订单
		{
			Vector<String []> list = null;
			try {
				int oid = Integer.parseInt(req.getParameter("oid"));
				String sql = "select * from olist where oid="+oid;
				list = Order_DB.getOrderList(sql);
			} catch (NumberFormatException nfe) {//输入订单号的格式不正确
				list = new Vector<String[]>(); //返回一个空的向量
			}
			req.setAttribute("list",list);
			req.getRequestDispatcher("../jsp/adminOrders.jsp").forward(req, res);
		}
		else if(action.equals("dealOrder"))//处理订单
		{
			String adname = (String)session.getAttribute("adname");
			String reason = req.getParameter("reason");
			String ostatus = req.getParameter("ostatus");
			int oid = Integer.parseInt(req.getParameter("oid"));
			//拼装SQL
			String sqla = "update olist set ostatus='"+ostatus+"',oreason='"+
						reason+"',odeal='"+adname+"'where oid="+oid;
			String sqlb = "update oinfo set ostatus='"+ostatus+"' where oid="+oid;
			boolean b = DB.update(sqla, sqlb);//执行更新
			if(b==true)
			{
				msg = "订单处理成功<br><br>"
					+ "<a href=servlet/OrderServlet?action=allOrders&&condition=1>返回</a>";
			}
			else{
				msg ="订单处理发生错误，处理失败";
			}
			req.setAttribute("msg",msg);
			req.getRequestDispatcher("../jsp/adinfo.jsp").forward(req, res);
		}
	}
}
 